package com.basker.pisces.domain.value.meta;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.util.Assert;

import com.basker.pisces.core.Element;
import com.basker.pisces.core.meta.annotation.CollectionField;
import com.basker.pisces.core.meta.annotation.DataObjectMeta;

import lombok.Getter;
import lombok.Setter;

/**
 * 描述枚举类型的元素
 *
 * @author hangwen
 */
@DataObjectMeta
public class EnumType extends Element {

    @CollectionField
    private List<EnumOption> options = new ArrayList<>();

    public EnumOption getOption(Object id) {
        Assert.notNull(id, "parameter 'id' is required");

        String strId = String.valueOf(id);
        Optional<EnumOption> optional = this.options.stream().filter(o -> o.getId().equals(strId)).findFirst();
        return optional.isPresent() ? optional.get() : null;
    }

    public List<EnumOption> getOptions() {
        return options;
    }

    public void setOptions(List<EnumOption> options) {
        this.options = options;
    }

    /**
     * 封装枚举类型集合的元素类
     *
     * @author hangwen
     * @see ResourceEnumTypeProvider
     */
    @Getter
    @Setter
    @DataObjectMeta
    public static class EnumTypeData {

        @CollectionField
        private List<EnumType> enumTypes = new ArrayList<EnumType>();
        ;
    }

}
